(function($) {
	
	$.fn.isLogin = function() {
		
		return $(this).each(function(){

			var $loginError = $('#login_error');
			
			// Stop chrome's autocomplete from making the bg of text-input yellow.
			if (navigator.userAgent.toLowerCase().indexOf("chrome") >= 0) {
				var _done = false,
					_interval = window.setInterval(function () {
					var autofills = $('input:-webkit-autofill');
					if (autofills.length > 0) {
						window.clearInterval(_interval); // stop polling
						autofills.each(function() {
							var clone = $(this).clone(true, true);
							$(this).after(clone).remove();
						});
						_done = true;
					} else if(_done) {
						window.clearInterval(_interval); // stop polling
				    }
				}, 20);
			}
			
			$('#login_form').validate({
				errorElement:'span',
				errorClass:'error',
				focusInvalid:true,
				onfocusout:false,
				onclick:false,
				onkeyup:false,
				submitHandler: function(form){
					form.submit();	
				},
				errorPlacement:function(error,element) {
					$('#account_error').remove();
					$('.error_wrap', $loginError).append(error).show();
					if($('span', $loginError).length !== 0){
						$loginError.show();
					}
				}, 
				success:function(element){
					if($('span', $loginError).length === 1){
						$loginError.hide();
					}
					element.remove();
				},
				rules: {
					username: {
						required: true,
						email:true
					},

					password:{
						required:true
					}
				},
				messages: {
					username: {
						required: "请输入邮箱",
						email: "邮箱格式不正确"
					},
					password: {
						required: "请输入密码"
					}
				}
			});
		});
	};
})(jQuery);